/** 
 * @(#)BeanUtilsExtends.java 1.0.0 2015年9月28日 下午6:15:32  
 *  
 * Copyright © 2015 善林金融.  All rights reserved.  
 */ 

package com.common;

import java.beans.PropertyDescriptor;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.BeanUtilsBean2;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.lang3.time.DateUtils;


/**
 * 对象转换工具类
 * @author  HuYaHui
 * @version $Revision:1.0.0, $Date: 2015年10月08日 下午8:40:57 $ 
 *
 */
public class BeanUtilsExtends extends BeanUtils {
	static {
		ConvertUtils.register(new DateConvert(), java.util.Date.class);
		ConvertUtils.register(new DateConvert(), java.sql.Date.class);
	}
	

//	public static <T>void merg(BaseEntity to,Map<String, Object> from,Class<T> cls) throws Exception {
//		//map-->bean
//		BaseEntity custInfo=(BaseEntity) JSONObject.parseObject(JSONObject.toJSONString(from), cls);
//		Object recordStatus=from.get("recordStatus");
//		if(StringUtils.isNotBlank(custInfo.getId())){
//			//如果是修改,清空字段默认值
//			custInfo.setId(null);
//			custInfo.setVersion(null);
//			custInfo.setCreateDate(null);
//		}
//		//合并相同字段,非空的属性值
//		merg(to, custInfo);
//		//from参数中如果设置了状态字段,使用from中的值
//		//如果未设置,则用默认值
//		if(recordStatus!=null && StringUtils.isNotBlank(recordStatus.toString())){
//			to.setRecordStatus(recordStatus.toString());
//		}
//	}
	
	/**
	 * 合并对象中非空的属性值
	 * @param to				目标对象(次),必须是bean,并且已经初始化
	 * @param from				源对象(主),可以是map,或者是bean
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public static Object merg(Object to, Object from){
		try {
			BeanUtilsBean beanUtils = BeanUtilsBean.getInstance();
			if (from instanceof Map) {
				Iterator<?> entries = ((Map<?, ?>) from).entrySet().iterator();
				while (entries.hasNext()) {
					Map.Entry entry = (Map.Entry) entries.next();
					String name = (String) entry.getKey();
					if (beanUtils.getPropertyUtils().isWriteable(to, name)) {
						beanUtils.copyProperty(to, name, entry.getValue());
					}
				}
			} else {
				PropertyDescriptor[] origDescriptors = beanUtils.getPropertyUtils()
						.getPropertyDescriptors(from);
				for (int i = 0; i < origDescriptors.length; i++) {
					String name = origDescriptors[i].getName();
					if ("class".equals(name)) {
						continue;
					}
					if (beanUtils.getPropertyUtils().isReadable(from, name)
							&& beanUtils.getPropertyUtils().isWriteable(to, name)) {
						try {
							Object value = beanUtils.getPropertyUtils()
									.getSimpleProperty(from, name);
							if (value != null && !value.toString().equals("")) {
								beanUtils.copyProperty(to, name, value);
							}
						} catch (NoSuchMethodException e) {
							e.printStackTrace();
						}
					}
				}
			}	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return to;
	}
	public static Object getFieldValue(Object bean, String name)
			throws Exception {
		return BeanUtilsBean2.getInstance().getPropertyUtils()
				.getProperty(bean, name);
	}

}

class DateConvert implements Converter{
	private String[] DATEARRAYS=
			new String[] {"yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm","yyyy-MM-dd"};
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Object convert(Class type, Object value) {
		if(value == null) {
			return null;
		}
		if(value instanceof Date) {
			return value;
		}
		if(value instanceof Long) {
			Long longValue = (Long) value;
			return new Date(longValue.longValue());
		}
		try {
			return DateUtils.parseDate(value.toString(), DATEARRAYS);
		} catch (Exception e) {
			throw new ConversionException(e);
		}
	}
	
}

